# -*- coding: utf-8 -*-
# @Time    : 2022/7/4 14:59
# @Author  : AUGUS
# @Email   : augus@jilin.com
# @File    : tasks.py
# @Software: PyCharm
import logging

from django.core.mail import send_mail

from celery_tasks.celery_main import app
from drf6 import settings

logger = logging.getLogger(__name__)


@app.task(name='send_email_task')
def send_mail_task(email, username, password, classes_name):
    """
    通知邮件任务
    :param email: 收件人邮箱
    :param username: 用户名
    :param password: 密码
    :param classes_name:班级名
    :return:
    """
    try:
        email_param = {
            'subject': '码趣教育—PROMOTE系统',
            'message': '',
            'html_message':
                f"""
                        <h2>欢迎加入码趣教育[{classes_name}]</h2>  
                        <h3>诚邀您使用码趣教育-PROMOTE系统</h3>
                        <p>其中包含:题目知识练习，技术学习讨论社区，相关书籍学习设备购物商场</p>
                        <p>您的登录用户名为:<span style="color:#116bb7">{username}</span></p>
                        <p>您的登录初始密码为:<span style="color:#116bb7">{password}</span></p>
                    """,
            'from_email': settings.EMAIL_HOST_USER,
            'recipient_list': [email]
        }
        res_email = send_mail(**email_param)
    except Exception as e:
        logger.error('入学通知邮件发送[异常][ email: %s, message: %s]' % (email, username))
    else:
        if res_email:
            logger.info('入学通知邮件发送[正常][ email: %s, username: %s]' % (email, username))
        else:
            logger.warning('入学通知邮件发送[失败][ email: %s, username: %s]' % (email, username))
